package cm

import (
	"encoding/hex"
	"errors"
	"log"
	//"math/big"
)

//FileStatus is the result read from contract
type FileStatus struct {
	// User      common.Address
	// UserId    string
	FileId   string
	FileHash string
	FileSm3  string
	FileMd5  string
	//BlkNum    *big.Int
	TimeStamp string //这个时间戳得确认一下是不是add时候的时间戳
}

//GetFileStatus find what the transaction filled in the past time.
func (cm *ClientManager) GetProofData(index string, mode int) (*FileStatus, error) {
	filedata := &FileStatus{}
	switch mode {
	case 1:
		byidres, err := cm.Contract.GetById(index)
		if err != nil {
			log.Println("GetById err:", err)
			return nil, err
		}
		filedata.FileId = byidres.FileId
		filedata.FileHash = byidres.FileHash
		filedata.FileSm3 = byidres.FileSm3
		filedata.FileMd5 = byidres.FileMd5
		//filedata.BlkNum = byidres.BlkNum
		filedata.TimeStamp = byidres.TimeStamp
		return filedata, nil
	case 2:
		//TODO:string to [32]byte还没有确定如果转换
		// byhashres, err := cm.Contract.GetByIdHash(common.HashToHex(index)) //TODO:string转[32]byte
		// if err != nil {
		// 	log.Println("GetByIdHash err:", err)
		// 	return nil, err
		// }
		// filedata.User = byhashres.User
		// filedata.UserId = byhashres.UserId
		// filedata.FileId = byhashres.FileId
		// filedata.FileHash = byhashres.FileHash
		// filedata.FileSm3 = byhashres.FileSm3
		// filedata.FileMd5 = byhashres.FileMd5
		// filedata.BlkNum = byhashres.BlkNum
		// filedata.TimeStamp = byhashres.TimeStamp
		// return filedata, nil
	case 3:
		bysm3, err := cm.Contract.GetBySm3(index)
		if err != nil {
			log.Println("GetBySm3 err:", err)
			return nil, err
		}
		strres := ""
		for _, v := range bysm3 {
			strres += hex.EncodeToString(v[:]) //TODO：这里得确认[][32]byte转换为string拼接起来会不会乱码
		}
		filedata.FileSm3 = strres
		return filedata, nil
	case 4:
		bymd5, err := cm.Contract.GetByMd5(index)
		if err != nil {
			log.Println("GetByMd5 err:", err)
			return nil, err
		}
		strres := ""
		for _, v := range bymd5 {
			strres += hex.EncodeToString(v[:]) //TODO：这里得确认[][32]byte转换为string拼接起来会不会乱码
		}
		filedata.FileMd5 = strres
		return filedata, nil
	default:
	}
	return nil, errors.New("unknown way")
}
